/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import DAO.exceptions.RollbackFailureException;
import Entidad.Recursossimple;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidad.Tarea;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author David
 */
public class RecursossimpleDAO implements Serializable {

    public RecursossimpleDAO(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Recursossimple recursossimple) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Tarea idtarea = recursossimple.getIdtarea();
            if (idtarea != null) {
                idtarea = em.getReference(idtarea.getClass(), idtarea.getIdtarea());
                recursossimple.setIdtarea(idtarea);
            }
            em.persist(recursossimple);
            if (idtarea != null) {
                idtarea.getRecursossimpleCollection().add(recursossimple);
                idtarea = em.merge(idtarea);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findRecursossimple(recursossimple.getIdrecursossimples()) != null) {
                throw new PreexistingEntityException("Recursossimple " + recursossimple + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Recursossimple recursossimple) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Recursossimple persistentRecursossimple = em.find(Recursossimple.class, recursossimple.getIdrecursossimples());
            Tarea idtareaOld = persistentRecursossimple.getIdtarea();
            Tarea idtareaNew = recursossimple.getIdtarea();
            if (idtareaNew != null) {
                idtareaNew = em.getReference(idtareaNew.getClass(), idtareaNew.getIdtarea());
                recursossimple.setIdtarea(idtareaNew);
            }
            recursossimple = em.merge(recursossimple);
            if (idtareaOld != null && !idtareaOld.equals(idtareaNew)) {
                idtareaOld.getRecursossimpleCollection().remove(recursossimple);
                idtareaOld = em.merge(idtareaOld);
            }
            if (idtareaNew != null && !idtareaNew.equals(idtareaOld)) {
                idtareaNew.getRecursossimpleCollection().add(recursossimple);
                idtareaNew = em.merge(idtareaNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = recursossimple.getIdrecursossimples();
                if (findRecursossimple(id) == null) {
                    throw new NonexistentEntityException("The recursossimple with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Recursossimple recursossimple;
            try {
                recursossimple = em.getReference(Recursossimple.class, id);
                recursossimple.getIdrecursossimples();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The recursossimple with id " + id + " no longer exists.", enfe);
            }
            Tarea idtarea = recursossimple.getIdtarea();
            if (idtarea != null) {
                idtarea.getRecursossimpleCollection().remove(recursossimple);
                idtarea = em.merge(idtarea);
            }
            em.remove(recursossimple);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Recursossimple> findRecursossimpleEntities() {
        return findRecursossimpleEntities(true, -1, -1);
    }

    public List<Recursossimple> findRecursossimpleEntities(int maxResults, int firstResult) {
        return findRecursossimpleEntities(false, maxResults, firstResult);
    }

    private List<Recursossimple> findRecursossimpleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Recursossimple.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Recursossimple findRecursossimple(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Recursossimple.class, id);
        } finally {
            em.close();
        }
    }

    public int getRecursossimpleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Recursossimple> rt = cq.from(Recursossimple.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
